
#include "reduce_sum_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"

namespace optiling {
/*
栈空间：
        0
      1   2
    11 12   2
  111 112 12 2
1111 1112 112 12 2 -----最大空间
retList空间
2 ^ (栈空间 - 1)
*/

inline void reduce(int64_t &stackCounter, int64_t &retListCounter,
                   int64_t numValuesToReduce, int64_t leafSize) {
  stackCounter = 1;
  retListCounter = 1;
  while (numValuesToReduce > leafSize) {
    numValuesToReduce /= 2;
    stackCounter += 1;
    retListCounter *= 2;
  }
  retListCounter /= 2;
  if (retListCounter == 0) {
    retListCounter = 1;
  }
}

static ge::graphStatus TilingFunc(gert::TilingContext *context) {
  ReduceSumTilingData tiling;
  context->SetBlockDim(1);
  auto xShape = context->GetInputShape(0)->GetStorageShape();
  auto size = xShape.GetShapeSize();
  tiling.set_size(size);
  int64_t x_shape[32] = {0};
  copyShape(xShape, x_shape);
  tiling.set_x_shape(x_shape);

  int64_t axes_shape[32] = {0};
  auto axesShape = context->GetInputShape(1)->GetStorageShape();
  copyShape(axesShape, axes_shape);
  tiling.set_axes_shape(axes_shape);

  auto ignore_nan = *(context->GetAttrs()->GetBool(1));

  // 计算最大需要的workspace size
  ge::DataType dtype = context->GetInputDesc(0)->GetDataType();
  auto dtypeSize = GetSizeByDataType(dtype);

  COMMON_TILING_GET_PLATFORM;
  COMMON_TILING_GET_UB_SIZE;
  tiling.set_ubSize(ub_size);

  tiling.SaveToBuffer(context->GetRawTilingData()->GetData(),
                      context->GetRawTilingData()->GetCapacity());
  context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
  std::cout << "-------------------------host--------------------------"
            << std::endl;
  print_shape_vector("x_shape", x_shape);
  print_shape_vector("axes_shape", axes_shape);
  std::cout << "size = " << tiling.get_size() << std::endl;
  std::cout << "ignore_nan = " << ignore_nan << std::endl;
  std::cout << "ub_size = " << tiling.get_ubSize() << std::endl;
  std::cout << "-------------------------host end----------------------"
            << std::endl;
  return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext *context) {
  const gert::Shape *x1_shape = context->GetInputShape(0);
  gert::Shape *y_shape = context->GetOutputShape(0);
  *y_shape = *x1_shape;
  return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops {
class ReduceSum : public OpDef {
public:
  explicit ReduceSum(const char *name) : OpDef(name) {
    this->Input("x")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Input("axes")
        .ParamType(REQUIRED)
        .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Output("y")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32, ge::DT_INT8})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat(
            {ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
    this->Attr("keep_dims").AttrType(OPTIONAL).Bool(false);
    this->Attr("ignore_nan").AttrType(OPTIONAL).Bool(false);
    this->Attr("dtype").AttrType(OPTIONAL).String("float");

    this->SetInferShape(ge::InferShape);

    this->AICore().SetTiling(optiling::TilingFunc);
    this->AICore().AddConfig("ascend310b");
  }
};

OP_ADD(ReduceSum);
} // namespace ops
